package org.mushroomdb.security.queries;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.mushroomdb.catalog.Catalog;
import org.mushroomdb.catalog.Table;
import org.mushroomdb.engine.EvaluationEngine;
import org.mushroomdb.exception.EvaluationEngineException;
import org.mushroomdb.exception.RDBMSRuntimeException;
import org.mushroomdb.query.Query;
import org.mushroomdb.security.Privilege;
import org.mushroomdb.security.SecurityService;
import org.mushroomdb.security.User;
import org.mushroomdb.service.ServiceManager;

public class GrantQuery extends SecurityQuery {
    private String tableName;

    private List privileges = new ArrayList();

    private String userName;

    /**
     * @param queryString
     */
    public GrantQuery(String queryString) {

        String query = queryString.replaceAll(",", " ");
        String[] keyWords = query.split(" ");
        List keyWordsList = new ArrayList();
        for (int i = 0; i < keyWords.length; i++) {
            String word = keyWords[i].trim();
            keyWordsList.add(word);
        }

        int tableIndex = getIndex("TABLE",keyWordsList);
        if (tableIndex != -1) {
            this.tableName = (String) keyWordsList.get(tableIndex + 1);
        }

        int onIndex = getIndex("ON",keyWordsList);
        for (int j = 1; j < onIndex; j++) {
            String word = keyWords[j].trim().toUpperCase();
            Privilege privilege = new Privilege(word);
            this.privileges.add(privilege);
        }

        //TODO HAcer esto menos cabeza
        int grantIndex = keyWordsList.lastIndexOf("GRANT");
        int grantIndex2 = keyWordsList.lastIndexOf("grant");
        int gr = grantIndex + grantIndex2;
        if (gr != -1) {
            this.privileges.add("GRANT");
        }

        int toIndex = getIndex("TO",keyWordsList);
        if (toIndex != -1) {
            this.userName = (String) keyWordsList.get(toIndex + 1);
        }

    }

    /**
     * @param string
     * @return
     */
    private int getIndex(String string, List list) {
        Iterator iter = list.iterator();
        int index = 0;
        boolean flag = true;
        while(iter.hasNext() && flag){
            String str = (String)iter.next();
            if(str.equalsIgnoreCase(string)){
                flag=false;
            }
            index++;
        }
        if(flag)
            return -1;
        else
            return index-1;
        
    }

    public Object execute(EvaluationEngine evaluationEngine) {
        SecurityService service = (SecurityService) ServiceManager
                .getInstance().getService(SecurityService.class);
        Catalog catalog = Catalog.getInstance();

        User user = null;
        user = service.getUserByName(this.userName);
        Table table = catalog.getTableByName(this.tableName);
        Iterator iter = this.privileges.iterator();
        while (iter.hasNext()) {
            Privilege privilege = (Privilege) iter.next();
            service.grant(user, privilege, table);
        }

        //return qty of privilege granted
        return  user;

    }

}
